package cn.xo68.boot.web.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.web.context.WebApplicationContext;

import java.net.InetAddress;
import java.util.Arrays;

/**
 * 启动运行信息输出
 * @author wuxie
 * @version 2019-9-2
 */
@Slf4j
public class StartupRuner implements ApplicationRunner {

    private static final String SERVER_PORT="server.port";

    @Autowired
    private Environment environment;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        StringBuilder logFormatSb=new StringBuilder();
        logFormatSb.append("\n---------------------------------\n");
        logFormatSb.append("      应用名称: {}\n");
        logFormatSb.append("          端口: {}\n");
        logFormatSb.append("ActiveProfiles: {}\n");
        logFormatSb.append("    Local: http://localhost:{}\n");
        logFormatSb.append(" External: http://{}:{}\n");
        logFormatSb.append(" swagger: http://{}:{}/swagger-ui.html\n");
        logFormatSb.append("------------------------------------");
        log.info(logFormatSb.toString(),
                environment.getProperty("spring.application.name"),
                environment.getProperty(SERVER_PORT),
                environment.getActiveProfiles(),
                environment.getProperty(SERVER_PORT),
                InetAddress.getLocalHost().getHostAddress(),
                environment.getProperty(SERVER_PORT),
                InetAddress.getLocalHost().getHostAddress(),
                environment.getProperty(SERVER_PORT));

        boolean isDev= Arrays.stream(environment.getActiveProfiles()).anyMatch((profile)->{
            return "dev".equals(profile);
        });

        log.info("是否dev环境：{}",isDev);

    }
}
